<?php namespace Admin\Controller;
class TypeController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\Type;
	}
	public function index(){
		$data=$this->model->get();
		View::with('data',$data);
		View::make();
	}
	public function add(){
		if(IS_POST){
			if($this->model->store()){
				View::success("添加成功",U('Type/index'));
			}
			View::error($this->model->getError());
		}
		View::make();
	} 
	public function edit(){
		$tid=Q('get.tid',0,'intval');
		if(IS_POST){
			if($this->model->edit()){
				View::success("修改成功",U('Type/index'));
			}
			View::error($this->model->getError());
		}
		$oldData=$this->model->where("tid={$tid}")->find();
		p($oldData);
		View::with('oldData',$oldData);
		View::make();
	}
	public function del(){
		$tid=Q('get.tid',0,'intval');
		$typeModel=new \Common\Model\TypeAttr;
		$typeData=$typeModel->where("type_tid={$tid}")->get();
		$cateModel=new \Common\Model\Cate;
		$cateData=$cateModel->where("type_tid={$tid}")->get();
		if($typeData){
			View::success("请先删除下面的类型属性");
		}
		else if($cateData){
			View::success("请先删除下面的分类");
		}
		else{
		$this->model->where("tid={$tid}")->delete();
		View::success("删除成功",U('Type/index'));
		}
		
	}
}